package main

import (
	"github.com/kataras/iris"
	"time"
	"global/structs"
	"github.com/lunny/log"
	"server/kline/model"
)

func onInfoRequest(ctx iris.Context) {
	c := bk.config
	info := structs.ServBaseInfo{
		Name: 		c.App.Name,
		Ip: 		c.Http.Ip,
		Port: 		c.Http.Port,
		LogOn: 		c.Log.On,
		Duration: 	time.Since(bk.startTime).String(),
	}
	ApiSuccess(ctx, info)
}

func onAddCoinPair(ctx iris.Context) {
	coinPair := ctx.URLParam("coin_pair")
	if coinPair == "" {
		log.Errorf("get coin pair field failed")
		ApiError(ctx, ERR_PARAMETER)
		return
	}

	if bk.data.CheckCoinPairExists(coinPair) {
		ApiSuccess(ctx, "success")
		return
	}

	item, err := model.CoinPairModel.GetFirst(coinPair)
	if err != nil {
		log.Errorf("get coin pair failed, coin pair: %s", coinPair)
		ApiError(ctx, ERR_PARAMETER)
		return
	}

	if item.Enabled != model.COIN_PAIR_ENABLED {
		log.Errorf("coin pair status not right, item: %v", item)
		ApiError(ctx, ERR_COINPAIR_NOT_ENABLE)
		return
	}

	// add to coin pair
	bk.data.AddCoinPair(*item)

	// kline
	if _, ok := bk.klineWorkers[coinPair]; !ok {
		w := NewKlineWorker(bk, *item)
		w.start()
		bk.RegistKlineWorker(item.Name, w)
	}

	// depth
	if _, ok := bk.depths[coinPair]; !ok {
		d := NewDepth(bk, *item)
		d.Run()
		bk.RegistDepth(item.Name, d)
	}

	ApiSuccess(ctx, "success")
}
